package com.mofofa.maozy.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.R
import com.mofofa.maozy.data.RevObjWalletListFcfa
import com.mofofa.maozy.databinding.DialogMaozyConfirmWalletMobileListBinding
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent

class FcfaRecheckWalletDialog : DialogFragment() {

    companion object {
        fun showMaozyDialog(
            fragmentManager: FragmentManager,
            itemList: List<RevObjWalletListFcfa>,
            listener: (String) -> Unit,
        ) {
            val dialog = FcfaRecheckWalletDialog()
            dialog.setMaozyDataMaozyList(itemList, listener)
            dialog.showNow(fragmentManager, FcfaRecheckWalletDialog::class.java.simpleName)
        }
    }

    lateinit var mMaozyViewMaozyBinding: DialogMaozyConfirmWalletMobileListBinding

    private lateinit var mFcfaRecheckWalletAdapter: FcfaRecheckWalletAdapter

    var mMaozyOnMaozyDialogMaozyListMaozyItemMaozyClickMaozyListener: (String) -> Unit =
        { listItem: String -> }
    val mMaozyDataMaozyList = mutableListOf<RevObjWalletListFcfa>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mMaozyViewMaozyBinding = DialogMaozyConfirmWalletMobileListBinding.inflate(layoutInflater)
        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubMaozyViews()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        mFcfaRecheckWalletAdapter.notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initMaozySubMaozyViews() {
        mFcfaRecheckWalletAdapter =
            FcfaRecheckWalletAdapter(mMaozyDataMaozyList)

        mMaozyViewMaozyBinding.rcDialogConfirmWalletMobileListList.layoutManager =
            LinearLayoutManager(context)
        mMaozyViewMaozyBinding.rcDialogConfirmWalletMobileListList.adapter =
            mFcfaRecheckWalletAdapter

        mMaozyViewMaozyBinding.submitDialogConfirmWalletMobileListConfirm.setOnClickListener {
            try {
                mMaozyViewMaozyBinding.submitDialogConfirmWalletMobileListConfirm.recordMaozyClickMaozyEvent()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            var ids = ""
            mMaozyDataMaozyList.forEach { item ->
                if (ids.isBlank()) {
                    ids = item.id
                } else {
                    ids = ids + "," + item.id
                }
            }
            if (ids.isNotBlank()) {
                mMaozyOnMaozyDialogMaozyListMaozyItemMaozyClickMaozyListener.invoke(ids)
            }
            dismiss()
        }

        mMaozyViewMaozyBinding.imageDialogConfirmWalletMobileListColse.setOnClickListener { dismiss() }
    }

    fun setMaozyDataMaozyList(
        itemList: List<RevObjWalletListFcfa>,
        listener: (String) -> Unit,
    ) {
        mMaozyOnMaozyDialogMaozyListMaozyItemMaozyClickMaozyListener = listener
        mMaozyDataMaozyList.clear()
        mMaozyDataMaozyList.addAll(itemList)
    }

}